package jp.sourceforge.akjrcp.videotools.subtitlemaker;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegCommandUtil;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegFLVParser;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegFileInfomation;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;
import jp.sourceforge.akjrcp.ffmpeg.SimpleCommandExecuter;


public class VideoThumbsCreater extends Thread{
	public static Log log=LogFactory.getLog(VideoThumbsCreater.class);
	 private IImageChecker imageChecker;

	 private ThumbnailPreference preference;
	 private SimpleCommandExecuter executer;
	 public VideoThumbsCreater(ThumbnailPreference preference) {
	this.preference=preference;
	}

	 
	 
	public void run(){
		
		 String ffmpeg=FFMpegUI.getFFmpegOrPureFFMpeg();
		 if(ffmpeg==null){
			 return;
		 }
		 if(!new File(preference.getBaseDir()).exists()){
			 File file=new File(preference.getBaseDir());
			 boolean ret=file.mkdirs();
			 if(ret){
				 log.info("make dir:"+file.getAbsolutePath());
			 }else{
				 log.warn("make output dir faild:"+file.getAbsolutePath());
			 }
		 }
		
		 String rates[]={"-r",""+preference.getFps()};
		 if(preference.getWidth()>0 &&preference.getHeight()>0){
			 String size=preference.getWidth()+"x"+preference.getHeight();
			rates=new String[]{"-r",""+preference.getFps(),"-s",size};
		 }
		 log.info("fps:"+preference.getFps());
		 String[] command=FFMpegCommandUtil.makeJpegOutputFileCommand(ffmpeg, preference.getVideoPath(), preference.getBaseDir()+preference.getBaseName()+"%5d"+"."+preference.getExtension(), rates);
		  executer=new SimpleCommandExecuter(command);
		// log.trace(FFMpegUtils.toPrintCommand(command));
		  //flv file info
		 executer.setPrinter(Akj_betasPlugin.getDefault().getProcessPrinter());
		 executer.execute();
		 
		//simple process executer.
		 
		 //call execute
		 //get duration and set fps and make
		 //done=true
	 }

	public IImageChecker getImageChecker() {
		return imageChecker;
	}

	public void setImageChecker(IImageChecker imageChecker) {
		this.imageChecker = imageChecker;
	}



	public SimpleCommandExecuter getExecuter() {
		return executer;
	}
}